import { message } from 'antd';
import { getUserInfo, setUserInfo } from '@/services/mySettings';
export default {
  namespace: 'myInfo',
  state: { my: {} },
  effects: {
    *getMyInfo({ payload }, { call, put }) {
      const res = yield call(getUserInfo, payload);
      if (+res.code === 0) {
        console.log('获取个人信息：', res);
        yield put({
          type: 'setData',
          payload: {
            my: res.data,
          },
        });
      }
    },
    *submitInfo({ payload }, { call, put }) {
      try {
        const res = yield call(setUserInfo, payload);
        console.log('提交结果：', res);
        if (+res.code === 0) {
          message.success('保存成功！');
        } else {
          message.error(res.message);
        }
      } catch (e) {
        console.log(e);
        message.error('保存失败，请稍后再试');
      }
    },
  },
  reducers: {
    setData(state, { payload }) {
      const newState = {
        ...state,
        ...payload,
      };
      return newState;
    },
  },
};
